package gaileenchess;

import java.util.LinkedList;

public class Pawn extends Piece
{
    public Pawn(Common.Side s, int r, int c)
    {
        super(s, Type.PAWN, r, c);
    }
    
    public boolean isValidMove(Common.Side playerSide, Board board, Board.Position pos1, Board.Position pos2)
    {

        Board.Position diff = pos1.diff(pos2);
        
        if(playerSide==Common.Side.WHITE)
        {
            if((diff.row==-2 && diff.column==0 && !board.isOccupied(pos2) && !this.hasMoved() && !board.isPathBlocked(pos1, pos2))|| (diff.row==-1 && diff.column==0 && !board.isOccupied(pos2)) || (diff.row==-1&&Math.abs(diff.column)==1&&board.isOccupied(pos2)))
            {
                return true;
            }
        }if(playerSide==Common.Side.BLACK)
        {
            if((diff.row==2 && diff.column==0 && !board.isOccupied(pos2) && !this.hasMoved() && !board.isPathBlocked(pos1, pos2))|| (diff.row==1 && diff.column==0 && !board.isOccupied(pos2)) || (diff.row==1&&Math.abs(diff.column)==1&&board.isOccupied(pos2)))
            {
                return true;
            }
        }
        
        
        return false;
        
    }
    
    public void getValidMoves(Common.Side playerSide, Board board, LinkedList<Board.Position.Move> pieces, boolean check, int player)
    {
        //TODO: en passant
        
        Board.Position moveTo = null;
        Board.Position current = getPosition();
        
        if(playerSide == Common.Side.WHITE)
        {
            moveTo = current.add(new Board.Position(1, 0));
            if(moveTo.isValid() && !board.isOccupied(moveTo))
            {
                getValidMovesCheckAndAdd(playerSide, board, pieces, moveTo, check, player);
            }
            
            moveTo = current.add(new Board.Position(2, 0));
            
            if(!this.hasMoved() && moveTo.isValid() && !board.isOccupied(moveTo)) //TODO: more than just hasMoved? check position?
            {
                getValidMovesCheckAndAdd(playerSide, board, pieces, moveTo, check, player);
            }
        }
        else
        {
            moveTo = current.add(new Board.Position(-1, 0));
            if(moveTo.isValid() && !board.isOccupied(moveTo))
            {
                getValidMovesCheckAndAdd(playerSide, board, pieces, moveTo, check, player);
            }
            
            moveTo = current.add(new Board.Position(-2, 0));
            
            if(!this.hasMoved() && moveTo.isValid() && !board.isOccupied(moveTo))
            {
                getValidMovesCheckAndAdd(playerSide, board, pieces, moveTo, check, player);
            }
        }
    }
}
